#!/bin/bash
if [[ $# -ne 2 ]]; then
    echo "The format is as follows:"
    echo "bash TRF-4.09.1.sh buildpath installpath"
    echo ":param buildpath: 应用构建绝对路径"
    echo ":param installpath: 应用安装绝对路径"
    exit 1
fi

[[ ! "$1" =~ ^/.* || ! -d "$1" || "$1" = "/" ]] &&  echo "请输入正确的构建路径" && exit 1
[[ ! "$2" =~ ^/.* || ! -d "$2" || "$2" = "/" ]] &&  echo "请输入正确的安装路径" && exit 1
if [[ "$1" == "$2" ]]; then echo "构建路径与安装路径不能相同，请输入正确的路径"; exit 1; fi

buildpath=$1
installpath=$2
[[ "$buildpath" =~ .*/$ ]] && buildpath=${buildpath%/*}
[[ "$installpath" =~ .*/$ ]] && installpath=${installpath%/*}
yum install -y  environment-modules csh time patch libatomic perl-Data-Dumper autoconf automake libtool numactl binutils systemd-devel valgrind flex wget

set -e
wget -t 10 -c -P $buildpath https://github.com/Benson-Genomics-Lab/TRF/archive/v4.09.1.tar.gz
tar -xvf $buildpath/v4.09.1.tar.gz -C $buildpath && rm -rf $buildpath/v4.09.1.tar.gz

cd $buildpath/TRF-4.09.1

echo "make start ++++++++++++++++++++++++++++++++++++++++++++++++++++++"
mkdir -p $installpath/TRF-4.09.1
CC=clang ./configure --prefix=$installpath/TRF-4.09.1
CFLAGS="-march=armv8.2-a -fsigned-char -ffp-contract=off"

make
make install
echo "install end ++++++++++++++++++++++++++++++++++++++++++++++++++++++"
echo $buildpath
echo $installpath
cp -r $buildpath/TRF-4.09.1/test_seqs.fasta $installpath/TRF-4.09.1/bin/

cat>"$installpath/TRF-4.09.1/TRF_modulefiles"<<EOF
#%Module1.0
conflict TRF
variable modfile [file normalize [info script]]
proc getModulefileDir {} {
    variable modfile
    set modfile_path [file dirname \$modfile]
    return \$modfile_path
}
set pwd [getModulefileDir]
set TRF \$pwd
setenv TRF \$TRF
prepend-path PATH \$TRF/bin
EOF

rm -rf $buildpath/TRF-4.09.1
